package com.xhs.zdl.demo.expandableListView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.xhs.zdl.demo.MyUtils;
import com.xhs.zdl.demo.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ExpandableListViewDemo extends AppCompatActivity {

    private ExpandableListView listView;
    private ArrayList<String> parent;
    private HashMap<String, List<String>> child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view_demo);

        findView();
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        //父item的数据源集合
        parent = new ArrayList<>();
        //父item为key，子List为value的map集合
        child = new HashMap<>();

        //存放单个child的集合
        List<String> list;
        //将child存入parent
        for (int i = 0; i < 4; i++) {
            //parent的数据源
            parent.add("parent" + (i+1));
            list = new ArrayList<>();
            list.add("child" + (i+1) + "-1");
            list.add("child" + (i+1) + "-2");
            list.add("child" + (i+1) + "-3");
            child.put("parent" + (i+1), list);
        }

        listView.setAdapter(new ExpandableListViewAdapter(this, parent, child));
        listView.setOnChildClickListener(onChildClickListener);
    }

    /**
     * 找到控件
     */
    private void findView() {
        listView = (ExpandableListView) findViewById(R.id.expandable_listView);
    }

    ExpandableListView.OnChildClickListener onChildClickListener = new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            MyUtils.Toast(ExpandableListViewDemo.this, "点击了：" + child.get(parent.get(i)).get(i1));

            return true;
        }
    };
}
